package com.ray.file.controller;

import com.ray.file.dto.UploadFileParamsDto;
import com.ray.file.dto.UploadFileResultDto;
import com.ray.file.service.MediaFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 文件处理业务类
 *
 * @author LiuBi
 * @version 1.0
 * @createDate 2024/5/27 16:51
 **/
@RestController("/files")
@Api("文件管理")
public class FileController {

    @Autowired
    private MediaFilesService mediaFilesService;

    @ApiOperation("上传小文件接口")
    @PostMapping(value = "/upload/courseware")
    public UploadFileResultDto upload(@RequestParam("file") MultipartFile upload) throws Exception {
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //文件大小
        uploadFileParamsDto.setFileSize(upload.getSize());
        //图片
        uploadFileParamsDto.setFileType("001001");
        //文件名称
        uploadFileParamsDto.setFilename(upload.getOriginalFilename());
        //文件大小
        long fileSize = upload.getSize();
        uploadFileParamsDto.setFileSize(fileSize);
        //创建临时文件
        File tempFile = File.createTempFile("minio", "temp");
        //上传的文件拷贝到临时文件
        upload.transferTo(tempFile);
        //文件路径
        String absolutePath = tempFile.getAbsolutePath();
        //上传文件
        return mediaFilesService.uploadFile(uploadFileParamsDto, absolutePath);
    }

}
